package com.mine.redis.util.utils.client;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


/**
 * @author weixiao
 * @date 2024/9/6 10:41
 */
public class MyRedisClient {

    public static void main(String[] args) {
        Socket socket = null;
        PrintWriter pw = null;
        BufferedReader br = null;
        try {
            // 1.创建Socket
            String host = "127.0.0.1";
            int port = 6379;
            socket = new Socket(host, port);
            // 2.获取输入输出流
            pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
            // 3.发送请求
            sendRequest(pw, "set", "name", "张三");
            // 4.解析响应
            Object response = handleResponse(br);
            System.out.println(response);

            sendRequest(pw, "mget", "name", "age", "gender");
            System.out.println(handleResponse(br));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static Object handleResponse(BufferedReader br) throws IOException {
        int firstChar = br.read();
        switch (firstChar) {
            case '+':
                return br.readLine();
            case '-':
                return br.readLine();
            case ':':
                return Long.parseLong(br.readLine());
            case '$':
                long strLen = Long.parseLong(br.readLine());
                if (strLen == 0 || strLen == -1) {
                    return "";
                }
                return br.readLine();
            case '*':
                return parseArray(br);
            default:
                throw new RuntimeException("格式错误");
        }
    }

    private static Object parseArray(BufferedReader br) throws IOException {
        int rows = Integer.parseInt(br.readLine());
        if (rows <= 0) {
            return null;
        }
        List<Object> result = new ArrayList<>(rows);
        for (int i = 0; i < rows; i++) {
            result.add(handleResponse(br));
        }
        return result;
    }

    private static void sendRequest(PrintWriter pw, String ... args) {
        int length = args.length;
        pw.println("*" + length); // 代表多行字符串
        for (String arg : args) {
            pw.println("$" + arg.getBytes(StandardCharsets.UTF_8).length);
            pw.println(arg);
        }
        pw.flush();
    }
}
